<?php
@session_start();
if(@$_POST['open'] != 'please'){
	exit;
}
include_once("../../php/include_all.php");

?>
<div class="row-fluid">
	<div class="span12">
		<h3 class="page-title">
            <b>Pembiayaan SPM</b>
        </h3>
		<div class="widget">
			<div class="widget-title" style="margin-top: 20px;">
                <h4><i class="icon-reorder"></i>Pembiayaan SPM</h4>
                <span class="tools">
                    <a href="javascript:;" class="icon-chevron-down"></a>
                </span>
            </div>
			<div class="widget-body form">
				<form id="form-pembiayaan" method="POST" action="" class="form-horizontal">
					<input type="hidden" name="act" value="save">
					<input type="hidden" id="id_pembiayaan" name="id_pembiayaan" value="0">
					<div class="control-group">
                        <label class="control-label">Tahun Anggaran <a href="#" id="tooltipx" data-toggle="tooltip" title="Pilih tahun anggaran"><i class="icon-question-sign"></i></a></label>
                        <div class="controls">
                            <select name="id_tahun" id="id_tahun">
								<option id="awal">Pilih salah satu</option>
								<?php
									$tahun = get_datas('SELECT DISTINCT t.id_thn_angg, t.thn_angg FROM spm_thn_angg t, spm_jenis_pelayanan j WHERE t.id_thn_angg = j.id_thn_angg');
									foreach($tahun as $tahun){
								?>
									<option value="<?php echo $tahun['id_thn_angg'] ?>"><?php echo $tahun['thn_angg'] ?></option>
								<?php
								}
								?>
							</select>
                        </div>
					</div>
					<div class="control-group">
                        <label class="control-label">Pelayanan</label>
                        <div class="controls label_pelayanan">
                            <input type='hidden' id='id_pelayanan' name='id_pelayanan' />
							<input type="text" id="nama_pelayanan" name="nama_pelayanan" class="input-xlarge" readonly />
                        </div>
					</div>
					<div class="control-group">
                        <label class="control-label">Indikator</label>
                        <div class="controls label_indikator">
							<input type='hidden' id='id_indikator' name='id_indikator' />
                            <input type="text" id="nama_indikator" name="nama_indikator" class="input-xlarge"  readonly />
                        </div>
					</div>
					<div class="control-group">
                        <label class="control-label">Kegiatan</label>
                        <div class="controls">
                            <a href="#modalwin" data-toggle="modal" id="pilih_kegiatan" class="btn btn-info" name="pilih_kegiatan">Pilih Kegiatan</a>&nbsp;&nbsp;<input type='hidden' id='idKeg' name='idKeg' /><span name="kegNama" id="kegNama" style="font-size:14px; font-family:Arial;"></span>
                        </div>
					</div>
					
					<div id="modalwin" class="modal hide fade" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
						<header class="modal-header">
							<a href="#" class="close geo-clear" data-dismiss="modal">x</a>
						<h3>Daftar Kegiatan</h3>
						</header>
						<div class="modal-body">
							
							<table class="table table-striped table-bordered">
							<thead>
								<tr>
									<th style="text-align:center;width:30px;">No</th>
									<th style="text-align:center;">Kegiatan</th>
									<th style="text-align:center;width:20px;">Pilih</th>
								</tr>
							</thead>
							<tbody id="tbody-kegiatan">
								<?php
									$no = 1;
									$bidang = $_SESSION['_id_bidang'];
									$kegiatan = get_datas("Select k.id_kegiatan, k.nama_kegiatan FROM spm_kegiatan k, spm_indikator i, spm_jenis_pelayanan p, spm_bidang_urusan b WHERE k.id_indikator = i.id_indikator AND i.id_pelayanan = p.id_pelayanan AND p.id_bidang = b.id_bidang AND b.id_bidang = '$bidang' ");
									foreach($kegiatan as $kegiatan){
									$as = (($kegiatan['id_kegiatan'] % 2) == 0)?'#FCFDFF':'#C7E7FC';
								?>
								<tr bgcolor="<?php echo $as;?>">
									<td style="text-align:center;"><?php echo $no;?></td>
									<td><?php echo $kegiatan['nama_kegiatan']?></td>
									<td style="text-align:center;"><input type="radio" class="radioKegiatan" id="nilai_<?php echo $kegiatan['id_kegiatan']?>" name="radioPem" onclick="clickMe(this);" value="<?php echo $kegiatan['id_kegiatan']?>"></td>
								</tr>
								<?php
								$id_keg = $kegiatan['id_kegiatan'];
								$no++;
								}
								?>
							</tbody>
							</table>
						</div>
					</div>
					
					<div id="modalkom" class="modal hide fade" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
						<header class="modal-header">
							<a href="#" class="close geo-clear" data-dismiss="modal">x</a>
						<h3>Daftar Komponen</h3>
						</header>
						<div class="modal-body">
							<input type="hidden" name='rowIdKom' id='rowIdKom' />
							<table class="table table-striped table-bordered">
							<thead>
								<tr>
									<th style="text-align:center;width:30px;">No</th>
									<th style="text-align:center;">Komponen</th>
									<th style="text-align:center;width:20px;">Pilih</th>
								</tr>
							</thead>
							<tbody id="tbody-komponen">
								<?php
									$no = 1;
									$bidang = $_SESSION['_id_bidang'];
									$komponen = get_datas("Select k.id_komponen, k.nama_komponen FROM spm_komponen k, spm_bidang_urusan b WHERE b.id_bidang = '$bidang' ");
									foreach($komponen as $komponen){
									$as = (($komponen['id_komponen'] % 2) == 0)?'#FCFDFF':'#C7E7FC';
								?>
								<tr bgcolor="<?php echo $as;?>">
									<td style="text-align:center;"><?php echo $no;?></td>
									<td><?php echo $komponen['nama_komponen']?></td>
									<td style="text-align:center;"><input type="radio" class="radioKomponen" id="nilai_<?php echo $komponen['id_komponen']?>" name="radioKom" onclick="clickMeYeah(this);" value="<?php echo $komponen['id_komponen']?>"></td>
								</tr>
								<?php
								$id_kom = $komponen['id_komponen'];
								$no++;
								}
								?>
							</tbody>
							</table>
						</div>
					</div>
					
					<div class="control-group">
						<label class="control-label">Sub Kegiatan</label>
						<div class="controls">
							<select name="id_subkegiatan" id="id_subkegiatan" class="input-xlarge" data-placeholder="Pilih Sub Kegiatan" tabindex="1" >
								<option id="awal">Pilih Salah Satu</option>
							</select>
						</div>
                    </div>
					
					<div class="control-group">
                        <label class="control-label">Komponen</label>
						<div id="kotak">
							<div id="cobadt"></div>
							<div id="input0"></div>
						</div>
						<div class="controls"> 
                            <a href="javascript:tambah_rincian();">Tambah Komponen Kegiatan</a>
                        </div>
					</div>
					
					<div class="control-group">
						<label class="control-label">Rekening</label>
						<div class="controls">
							<select name="id_rekening" id="id_rekening">
								<option id="awal">Pilih salah satu</option>
								<?php
									$rekening = get_datas('SELECT * FROM spm_rekening');
									foreach($rekening as $rekening){
								?>
									<option value="<?php echo $rekening['id_rekening'] ?>"><?php echo $rekening['nama_rekening'] ?></option>
								<?php
								}
								?>
							</select>
						</div>
                    </div>
					
					<div class="control-group">
						<label class="control-label">Sumber Dana</label>
						<div class="controls">
							<select name="sumberdana" id="sumberdana">
								<option id="awal">Pilih salah satu</option>
								<option id="APBN - Pusat">APBN - Pusat</option>
								<option id="APBN - DAK">APBN - DAK</option>
								<option id="APBN - DEKON">APBN - DEKON</option>
								<option id="APBN - TUGAS PEMBANTUAN">APBN - TUGAS PEMBANTUAN</option>
								<option id="APBN - BOK">APBN - BOK</option>
								<option id="APBD - PROVINSI">APBD - PROVINSI</option>
								<option id="APBD - KABUPATEN/ KOTA">APBD - KABUPATEN/ KOTA</option>
								<option id="PHLN">PHLN</option>
								<option id="APBD - KABUPATEN/ KOTA LAINNYA">APBD - KABUPATEN/ KOTA LAINNYA</option>
								<option id="MANDIRI">MANDIRI</option>
								<option id="JAMKESMAS">JAMKESMAS</option>
							</select>
						</div>
                    </div>
					
					<div class="control-group">
                        <label class="control-label">Rumus</label>
                        <div class="controls">
                            <input type="text" id="rumus" name="rumus" class="input-xlarge"/><input style="margin-left:10px" class="btn btn-primary" type="button" value="hitung" onCLick="hitung()"><input style="margin-left:10px" type="text" id="hasil" name="hasil" class="input-medium" readonly />
                        </div>
					</div>
					<div class="control-group">
                        <label class="control-label">Operator</label>
                        <div class="controls">
                            <a class="btn btn-primary btn-tambah operator">+</a>
                            <a class="btn btn-primary btn-kurang operator">-</a>
                            <a class="btn btn-primary btn-kali operator">*</a>
                            <a class="btn btn-primary btn-bagi operator">/</a>
                            <a class="btn btn-primary btn-buka operator">(</a>
                            <a class="btn btn-primary btn-tutup operator">)</a>
                            <a class="btn btn-primary btn-clear operator">C</a>
                        </div>
					</div>
					
					
					<div class="form-actions">
                        <button type="button" class="btn btn-primary btn-simpan">Simpan</button>
						<span id="result"></span>
                    </div>
				</form>
			</div>
		</div>
	</div>
	<div class="row-fluid">
        <div class="span12">
			<div class="widget" style="">
                <div class="widget-title">
                    <h4><i class="icon-reorder"></i>Tabel Pembiayaan</h4>
                    <span class="tools">
                        <a href="javascript:;" class="icon-chevron-down"></a>
                    </span>
                </div>
				<div class="widget-body">
                    <table class="table table-striped table-bordered">
                    <thead>
                        <tr>
                            <th class="hidden-phone" style="width:20px; text-align:center;">No</th>
                            <th style="text-align:center;">Kegiatan</th>
                            <th style="text-align:center;">Sub Kegiatan</th>
                            <th style="text-align:center;">Rekening</th>
                            <th style="text-align:center;">Sumber Dana</th>
                            <th style="text-align:center;">Jumlah</th>
							<th class="hidden-phone" style="text-align:center;">Edit</th>
                        </tr>
                    </thead>
                    <tbody id="tbody-pembiayaan">
						<?php
							$no = 1;
							$data = get_datas("SELECT p.id_pembiayaan, k.nama_kegiatan, s.nama_subkegiatan, r.nama_rekening, p.sumber_dana, p.jumlah FROM spm_kegiatan k, spm_subkegiatan s, spm_rekening r, spm_pembiayaan p WHERE p.id_kegiatan = k.id_kegiatan AND p.id_subkegiatan = s.id_subkegiatan AND p.id_rekening = r.id_rekening");
							foreach ($data as $data){
						?>
						<tr>
							<td class="hidden-phone" style="text-align:center;"><?php echo $no?></td>
							<td><?php echo $data['nama_kegiatan'] ?></td>
							<td><?php echo $data['nama_subkegiatan'] ?></td>
							<td><?php echo $data['nama_rekening'] ?></td>
							<td><?php echo $data['sumber_dana'] ?></td>
							<td><?php echo $data['jumlah'] ?></td>
							<td class="hidden-phone" style="width:200px; text-align:center;">	<a class="btn btn-info btn-edit" name="<?php echo $data['id_pembiayaan']?>"><i class="icon-edit icon-white"></i>&nbsp; Edit</a>
									<a class="btn btn-danger btn-delete" name="<?php echo $data['id_pembiayaan']?>"><i class="icon-trash icon-white"></i>&nbsp; Delete</a>
							</td>
						</tr>
						<?php
						$no++;
						}
						?>
					</tbody>
					</table>
				</div>
			</div>
		</div>
	</div>
</div>
<script>

$('#tooltipx').tooltip('hide')

var urls = 'master_pembiayaan/pembiayaan_action.php';

    jQuery(document).ready(function() {
         // initiate layout and plugins
        App.init();
    });
	
counter = 0;
function tambah_rincian(){
	var ccc = $('#kotak').find("input[name='rows[]']");
	var xcounter = ccc.length+1;
	
	counterNext = counter +1;
	document.getElementById("input"+counter).innerHTML="<table align='' style='margin-left:165px'><tr><td><i class='icon-pencil' onclick='insertNilai(this);' style='color:blue;cursor:pointer;'></i></td><td align='center'><input type='hidden' name='id[]'><label style='width:30px;' name='nomor[]' >"+xcounter+"</label></td>           <td><a href='#modalkom' data-toggle='modal' id='id_"+xcounter+"' class='btn btn-info' onclick='pilihKom(this);' name='id_kom'>Pilih Komponen</a>&nbsp;&nbsp;<input type='hidden' id='id_komponen"+xcounter+"' name='id_komponen[]' /></td><td  style='width:400px'><span id='nama_komponen"+xcounter+"' name='nama_komponen[]' style='font-size:14px; font-family:Arial; margin-right:20px;'></span></td>       <td><input type='text' style='width:100px;' name='volume[]' placeholder='Volume / Harga'></td><input type='hidden' name='rows[]'  value='"+xcounter+"'><td class='center'><i class='icon-remove red remRumus' style='color:red;cursor:pointer;'></i></td></tr></table><div id=\"input"+counterNext+"\"></div>";
	counter++;
	remRow();
}

function clickMe(a){
	var val = $(a).val();
	var uraian = $(a).parent().prev().html();
	console.log(val+" // "+uraian);
	$('#idKeg').val(val);
	$('#kegNama').html(uraian);
	$('#modalwin').modal('hide');
};

function pilihKom(a){
	var id = $(a).attr('id').replace('id_','');
	$('#rowIdKom').val(id);
	console.log(id);
}

function clickMeYeah(a){
	var val = $(a).val();
	var id = $('#rowIdKom').val();
	var uraian = $(a).parent().prev().html();
	console.log(val+" // "+uraian);
	$('#id_komponen'+id).val(val);
	$('#nama_komponen'+id).html(uraian);
	$('#modalkom').modal('hide');
	$('input[name=radioKom]').attr('checked', false);
};

function remRow(){
	$('.remRumus').click(function(){
		var table = $(this).parent().parent();
		console.log("tes : "+table);
		table.remove();
	});
		
}
function insertNilai(a){
		var textRumus = $('#rumus');
		var table = $(a).parent().parent().children();
		var input = $(table[4]).children();		
		console.log($(input[0]).val());
		var nilai = textRumus.val()+$(input[0]).val();
		textRumus.val(nilai);
}
$('.operator').click(function(){
	var operator = $(this).html();	
	var textRumus = $('#rumus');				
	console.log(operator);
	var nilai = textRumus.val()+operator;
	textRumus.val(nilai)
	if(operator == 'C'){
		textRumus.val('');
	}
});

$('.radioKegiatan').click(function(){
var post = $.post(urls,{act:'get_subkegiatan',idkegi:this.value});
	post.done(function(data){
		$('#id_subkegiatan').html(data);
	});
});

$('.radioKegiatan').click(function(){
var post = $.post(urls,{act:'get_indikator',idkegi:this.value});
	post.done(function(data){
		$('.label_indikator').html(data);
		console.log(data);
	});
});

$('.radioKegiatan').click(function(){
var post = $.post(urls,{act:'get_pelayanan',idkegi:this.value});
	post.done(function(data){
		$('.label_pelayanan').html(data);
	});
});

$('.cariPro').click(function(){
	var pro = $('#cari').val();
	source = "search.php?pro=" + pro;
	var cobapro = $.get(source);
	cobapro.done(function(data){
		$('#tbody-kegiatan').html(data);
	});
});

function hitung(){
	var hasil = 0;
	var data = document.getElementById('rumus').value;
	var comp = 'hasil = '+data+';';
	eval(comp);
	document.getElementById('hasil').value = hasil;
}

var hasil1 = $('#result');
$('.btn-simpan').click(function(){
	var btn = $(this);
	var form = $('#form-pembiayaan');
	var data = form.serializeArray();
	var post = $.post(urls, data);
	
	post.done(function(res){
		var result = res.split('__');
		if(result.length == 3){
			if(result[0] == 'success'){
				hasil1.html('<font color="green">tersimpan... </font>');
				setTimeout(function(){
					hasil1.html('');
				}, 1500);
				
				var tbody = $('#tbody-pembiayaan');
				tbody.html(result[2]);
				go();
				$('#cobadt').html("");
				$('#id_pelayanan').val("");
				$('#nama_pelayanan').val("");
				$('#id_indikator').val("");
				$('#nama_indikator').val("");
				$('#kegNama').html("");
				$('#rumus').val("");
				$('#hasil').val("");
				document.getElementById("awal").selected = true;
				$('#id_pembiayaan').val(0);
			}else{
				hasil1.html('<font color="red">'+res+'</font>');
			}
		}else{
			hasil1.html('<font color="red">'+res+'</font>');
		}
	});
});

function go(){
	$('.btn-delete').click(function(){
		var btn = $(this);
		var pid = this.name;
		var load = $(this);
		bootbox.confirm("Anda akan menghapus data pembiayaan ini?", function(result) {
               if(result==true){
                   if(pid!=0){
                       var post = $.post(urls,{act:"delete",id_pembiayaan:pid});
                       post.done(function(data){
                           var jadi = data.split('__');
							if(jadi.length == 3){
								if(jadi[0] == 'success'){
									hasil1.html('<font color="green">Data Telah Dihapus...</font>');
									setTimeout(function(){
										hasil1.html('');
									}, 1500);
									var tbody = $('#tbody-pembiayaan');
									tbody.html(jadi[2]);
									go();
								//btn.parent().parent().remove();
								}
							}
                       });
                       load.html('<i class="icon-trash icon-white"></i>&nbsp; Delete');
                   }
               }else{
                   load.html('Delete');
               }
           });
	});
	
	$('.btn-edit').click(function(){
           var id_pembiayaan= this.name;
           var form = $('#form-pembiayaan');
           var post = $.post(urls,{act:'edit',id_pembiayaan:id_pembiayaan});
           post.done(function(res){
               var value = res.split('__');
               //alert(res);
               $('#id_pembiayaan').val(value[0]);
               form.find('select[name="id_tahun"]').val(value[1]);
               form.find('input[name="nama_pelayanan"]').val(value[2]);
               form.find('input[name="nama_indikator"]').val(value[3]);
               $('#idKeg').val(value[4]);
               $('#kegNama').html(value[5]);
			   $('#id_subkegiatan').html(value[13]);
               form.find('select[name="id_subkegiatan"]').val(value[6]);
			   $('#cobadt').html(value[14]);
               form.find('select[name="id_rekening"]').val(value[7]);
			   form.find('select[name="sumberdana"]').val(value[8]);
			   form.find('input[name="rumus"]').val(value[9]);
			   form.find('input[name="hasil"]').val(value[10]);
			   $('#id_pelayanan').val(value[11]);
			   $('#id_indikator').val(value[12]);
			   
           });
       });
}

go();

</script>